package Algorithm.dfs;

import DataStructure.tree.TreeNode;

/**
 * 1448. 统计二叉树中好节点的数目
 */
public class GoodNodes {

    int count = 0;
    public int goodNodes(TreeNode root) {
        if(root == null) return 0;
        preOrder(root, root.val);
        return count;
    }

    /**
     * 在递归过程中每个方法栈帧中用max记录当前路径最大值
     */
    public void preOrder(TreeNode root, int max) {
        if(root == null) return;
        if(root.val >= max) {
            count++;
            max = root.val;
        }
        preOrder(root.left, max);
        preOrder(root.right, max);
    }
}
